import Web3 from 'web3'
import USDT from './bnbUsdt.json'
import DAPP from './dapp.json'
import { Toast } from 'vant';
//正式
const ADDRESS_DAPP ="0x55d5b25AD74Be7504AebA95FBDc262615862A505"
const ADDRESS_DAPPS ="0xa53aB1367D7a692Bade5BaF25b15db34881f30AE"
const ADDRESS_CAKE ="0xa9251ca9de909cb71783723713b21e4233fbf1b1"
const ADDRESS_USDT ="0x55d398326f99059fF775485246999027B3197955"

// const ADDRESS_DAPP ="0x6acb369ba60639FF0014E7F9cBc2D50ab7bbD47d"
// const ADDRESS_DRC ="0xF7b8757b094001Df138f0b2ac1069c564061889a"
// const ADDRESS_USDT ="0xE37B05A2C1B52C1fbACCFA8794e094E1Ad7c328D"

let ethereum = window.ethereum
    let i = setInterval(() => {
    if ((typeof window.ethereum) !== 'undefined') {
        ethereum = window.ethereum      
        handleWatch()  
        clearInterval(i)  
    }
    }, 1000);

export async function set(){
    ethereum = window.ethereum
}

export async function handleWatch () {
    if (!window.ethereum) {
        //console.log('Metamask is not installed!')
        return false
      }
    window.ethereum.on('accountsChanged',  (accounts) => {
      if(accounts[0]){
        // let web3s = new Web3(ethereum);
        // let msg = web3s.utils.keccak256('授权签名');
        // let ht= await web3s.eth.sign(msg, accounts[0])
        // .then(res=>{return res});  
        // if(ht){
      sessionStorage.setItem('userWallet',accounts[0])  
    //   store.commit("userWallet", accounts[0]);  
    }else{
        sessionStorage.removeItem('userWallet')
      }     
    })
  }
export async function connectTron() {
    let web3s = new Web3(ethereum);
    //console.log(web3s.utils.toDecimal(ethereum.chainId),ethereum.chainId);
    if (web3s.utils.toDecimal(ethereum.chainId)!='0x38') {
        
        try {
            await window.ethereum.request({
                method: 'wallet_switchEthereumChain',
                params: [{
                    chainId: Web3.utils.numberToHex(56), // 目标链ID
                }]
            })
            // //console.log('now',ethereum.chainId);
            return true
        } catch (e) {
            // //console.log('now',e);
            if (e.code == 4902) {
                try {
                    await window.ethereum.request({
                        method: 'wallet_addEthereumChain',
                        params: [{
                            chainId: Web3.utils.numberToHex(56), // 目标链ID
                            chainName: 'Arbitrum One',
                            nativeCurrency: {
                                name: 'BNB',
                                symbol: 'BNB',
                                decimals: 18
                            },
                            rpcUrls: ['https://arbitrum-mainnet.infura.io'], // 节点
                            blockExplorerUrls: ['https://explorer.arbitrum.io']
                        }]
                    })
                    return true
                } catch (ee) {
                    //console.log('//---------', ee);

                }
            } else if (e.code === 4001) return false
        }    
    }
    const eth_requestAccounts = await ethereum.request({ method: "eth_requestAccounts" });
    // return ["0x82c786fF41D9DAC121c72ea740324AEa059e3b14"]
    return eth_requestAccounts
}

export function Sign(address) {
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let msg = web3s.utils.utf8ToHex('Welcome to BtsProtocol');
        //console.log(msg,address);
        let ht= await web3s.eth.personal.sign(msg, address)
        .then(res=>{return res}).catch(err=>{
          // Toast(err)
          //console.log(err);
          return false});
       return ht?ht:false
  };
  return run();
  }

//   获取用户信息
export async function getUser(address) {  
    let run = async () => {
        ethereum?set():""
     let web3s = new Web3(ethereum); 
     let unioContract= await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);  
 console.log(address);
    let result = await unioContract.methods.users(address).call(); 
    if(result){
        return  result
    }else{
        return 0
    }
  };
  return run();
}
// 平台提现
export  function towToken(address,user,num){
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
       let gas =await web3s.eth.getGasPrice()
       let amount = web3s.utils.toWei(num + '', 'ether');
       console.log(ADDRESS_CAKE,user,amount);
        let result = await unioContract.methods.wToken(ADDRESS_CAKE,user,amount).send({from:address, gasPrice:gas}).catch(err => {
            //console.log(err.message)
            return false
        });
        if(result){
            return result.transactionHash
        }else{
            return false
        }
  };
  return run();
}
// 提现合约地址提现
export  function towTokenD(address){
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
       let gas =await web3s.eth.getGasPrice()
       //console.log(amount);
        let result = await unioContract.methods.wTokenD().send({from:address, gasPrice:gas}).catch(err => {
            //console.log(err.message)
            return false
        });
        if(result){
            return result.transactionHash
        }else{
            return false
        }
  };
  return run();
}

// 设置黑名单
export  function toSetBlackUser(address,user,enable){
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
       let gas =await web3s.eth.getGasPrice()
        let result = await unioContract.methods.setBlackUser(user,enable).send({from:address, gasPrice:gas}).catch(err => {
            //console.log(err.message)
            return false
        });
        if(result){
            return result.transactionHash
        }else{
            return false
        }
  };
  return run();
}

// 设置白名单
export  function toSetWhiteUser(address,user,enable){
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
       let gas =await web3s.eth.getGasPrice()
       console.log(address,user,enable);
        let result = await unioContract.methods.setWhiteUser(user,enable).send({from:address, gasPrice:gas}).catch(err => {
            //console.log(err.message)
            return false
        });
        if(result){
            return result.transactionHash
        }else{
            return false
        }
  };
  return run();
}
// 设置用户等级
export  function toSetUserLevel(address,user,level){
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
       let gas =await web3s.eth.getGasPrice()
        let result = await unioContract.methods.setUserLevel(user,level).send({from:address, gasPrice:gas}).catch(err => {
            //console.log(err.message)
            return false
        });
        if(result){
            return result.transactionHash
        }else{
            return false
        }
  };
  return run();
}

// 设置白名单奖池比例
export  function toSetNodePool(address,userRatio,platRatio){
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
       let gas =await web3s.eth.getGasPrice()
        let result = await unioContract.methods.setNodePool(userRatio,platRatio,1).send({from:address, gasPrice:gas}).catch(err => {
            //console.log(err.message)
            return false
        });
        if(result){
            return result.transactionHash
        }else{
            return false
        }
  };
  return run();
}

// 清除每日业绩
export  function toResetRankDay(address){
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
       let gas =await web3s.eth.getGasPrice()
        let result = await unioContract.methods.resetRankDay().send({from:address, gasPrice:gas}).catch(err => {
            //console.log(err.message)
            return false
        });
        if(result){
            return result.transactionHash
        }else{
            return false
        }
  };
  return run();
}

// 清除每日收益
export  function toCleanDayReward(address){
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
       let gas =await web3s.eth.getGasPrice()
        let result = await unioContract.methods.cleanDayReward().send({from:address, gasPrice:gas}).catch(err => {
            //console.log(err.message)
            return false
        });
        if(result){
            return result.transactionHash
        }else{
            return false
        }
  };
  return run();
}

// 静态动态
export  function toRewardStatic(address){
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
       let gas =await web3s.eth.getGasPrice()
        let result = await unioContract.methods.rewardStatic().send({from:address, gasPrice:gas}).catch(err => {
            //console.log(err.message)
            return false
        });
        if(result){
            return result.transactionHash
        }else{
            return false
        }
  };
  return run();
}

// 公排分红
export  function toRewardPub(address){
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
       let gas =await web3s.eth.getGasPrice()
        let result = await unioContract.methods.rewardPub().send({from:address, gasPrice:gas}).catch(err => {
            //console.log(err.message)
            return false
        });
        if(result){
            return result.transactionHash
        }else{
            return false
        }
  };
  return run();
}
// 白名单分红
export  function toRewardWhite(address){
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
       let gas =await web3s.eth.getGasPrice()
        let result = await unioContract.methods.rewardWhite().send({from:address, gasPrice:gas}).catch(err => {
            //console.log(err.message)
            return false
        });
        if(result){
            return result.transactionHash
        }else{
            return false
        }
  };
  return run();
}


//   获取用户订单信息
export async function getOrders(address,type) {  
    let run = async () => {
        ethereum?set():""
     let web3s = new Web3(ethereum); 
     let unioContract= await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);  
 
    let result = await unioContract.methods.getOrders(address,type).call(); 
    if(result){
        return  result
    }else{
        return []
    }
  };
  return run();
}

//   获取用户信息
export async function getUserInfos(address) {  
    let run = async () => {
        ethereum?set():""
     let web3s = new Web3(ethereum); 
     let unioContract= await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);  
 
    let result = await unioContract.methods.userInfos(address).call(); 
    if(result){
        return  result
    }else{
        return []
    }
  };
  return run();
}

//   获取白名单用户
export async function getWhiteList(address) {  
    let run = async () => {
        ethereum?set():""
     let web3s = new Web3(ethereum); 
     let unioContract= await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);  
 
    let result = await unioContract.methods.whiteList(address).call(); 
    return  result
   
  };
  return run();
}

//   获取白名单用户
export async function getBlackList(address) {  
    let run = async () => {
        ethereum?set():""
     let web3s = new Web3(ethereum); 
     let unioContract= await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);  
 
    let result = await unioContract.methods.blackList(address).call(); 
   return  result
  };
  return run();
}

//   获取白名单池信息
export async function getWhitePool() {  
    let run = async () => {
        ethereum?set():""
     let web3s = new Web3(ethereum); 
     let unioContract= await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);   
    let result = await unioContract.methods.whitePool().call(); 
    return  result
 
  };
  return run();
}

//   获取平台总数据
export async function getTotalData() {  
    let run = async () => {
        ethereum?set():""
     let web3s = new Web3(ethereum); 
     let unioContract= await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);   
    let result = await unioContract.methods.totalData().call(); 
    return  result
 
  };
  return run();
}

//   获取平台总数据
export async function getTotalDatas() {  
    let run = async () => {
        ethereum?set():""
     let web3s = new Web3(ethereum); 
     let unioContract= await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);   
    let result = await unioContract.methods.totalData2().call(); 
    return  result
 
  };
  return run();
}
// 我的社区显示直推信息
export async function getDirectReferralsPage(address,page,size) {  
    let run = async () => {
        ethereum?set():""
     let web3s = new Web3(ethereum); 
     let unioContract= await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);  
     //console.log(address);
    let result = await unioContract.methods.getDirectReferralsPage(address,page,size).call(); 

    if(result){
        return  result
    }else{
        return []
    }
  };
  return run();
}




// 设置糖浆地址
export  function toSetWalletA(address,user){
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
       let gas =await web3s.eth.getGasPrice()
        let result = await unioContract.methods.setAveAddress(user).send({from:address, gasPrice:gas}).catch(err => {
            //console.log(err.message)
            return false
        });
        if(result){
            return result.transactionHash
        }else{
            return false
        }
  };
  return run();
}

// 查询新增业绩
export async function getRankMappingDay(address) {  
    let run = async () => {
        ethereum?set():""
     let web3s = new Web3(ethereum); 
     let unioContract= await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);   
    let result = await unioContract.methods.rankMappingDay(address).call(); 
    return  result
 
  };
  return run();
}
// 查询新增业绩
export async function getWalletA() {  
    let run = async () => {
        ethereum?set():""
     let web3s = new Web3(ethereum); 
     let unioContract= await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);   
    let result = await unioContract.methods.aveAddress().call(); 
    return  result
 
  };
  return run();
}

export async function getAdmin() {  
    let run = async () => {
        ethereum?set():""
     let web3s = new Web3(ethereum); 
     let unioContract= await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);   
    let result = await unioContract.methods.role().call(); 
    return  result
 
  };
  return run();
}
// 查询余额
export function getCake() {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(USDT, ADDRESS_CAKE);
          let result = await unioContract.methods.balanceOf(ADDRESS_DAPP).call();
          if(result){
            return  web3s.utils.fromWei(result+'', 'ether');
        }else{
            return 0
        }
    };
    return run();
  }
// 查询余额
export function getCakes() {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(USDT, ADDRESS_USDT);
          let result = await unioContract.methods.balanceOf(ADDRESS_DAPP).call();
          if(result){
            return  web3s.utils.fromWei(result+'', 'ether');
        }else{
            return 0
        }
    };
    return run();
  }


  

// 设置每日发放代币数量
export  function toSetBaseReward(address,num){
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let amount = web3s.utils.toWei(num + '', 'ether');
        let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
       let gas =await web3s.eth.getGasPrice()
        let result = await unioContract.methods.setBaseReward(amount).send({from:address, gasPrice:gas}).catch(err => {
            //console.log(err.message)
            return false
        });
        if(result){
            return result.transactionHash
        }else{
            return false
        }
  };
  return run();
}

export async function getBaseReward() {  
    let run = async () => {
        ethereum?set():""
     let web3s = new Web3(ethereum); 
     let unioContract= await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);   
    let result = await unioContract.methods.baseReward().call(); 
    return  web3s.utils.fromWei(result+'', 'ether');
 
  };
  return run();
}

export async function getQueryLevelCount() {  
    let run = async () => {
        ethereum?set():""
     let web3s = new Web3(ethereum); 
     let unioContract= await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);   
     const owner = await unioContract.methods.owner().call();
    let d1 = await unioContract.methods.queryLevelCount(1).call({from:owner}); 
    let d2 = await unioContract.methods.queryLevelCount(2).call({from:owner}); 
    let d3 = await unioContract.methods.queryLevelCount(3).call({from:owner}); 
    let d4 = await unioContract.methods.queryLevelCount(4).call({from:owner}); 
    let d5 = await unioContract.methods.queryLevelCount(5).call({from:owner}); 
    let d6 = await unioContract.methods.queryLevelCount(6).call({from:owner}); 
    let d7 = await unioContract.methods.queryLevelCount(7).call({from:owner}); 
    let d8 = await unioContract.methods.queryLevelCount(8).call({from:owner}); 
    let d9 = await unioContract.methods.queryLevelCount(9).call({from:owner}); 
    let d10 = await unioContract.methods.queryLevelCount(10).call({from:owner}); 
    return  {d1,d2,d3,d4,d5,d6,d7,d8,d9,d10}
 
  };
  return run();
}





















export function getHoder() {
    let run = async () => {
          let web3s = new Web3("https://bscrpc.com");
          const unioContract =await new web3s.eth.Contract(USDT, ADDRESS_DRC); 
            const events = await unioContract.getPastEvents('Transfer', { fromBlock: 0, toBlock: 'latest' }); 
        //   //console.log(events,'limit exceeded');
            const holders = new Set(); 
            events.forEach((event) => { 
                holders.add(event.returnValues.to); }); 
            //console.log(`Total holders: ${holders.size}`);         
          let result = holders.size;
         return result 
    };
    return run();
  }
  export function getCanBind(address) {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
          let result = await unioContract.methods.canBind(address).call();
         return result
    };
    return run();
  }
  export function getProductList() {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
          let result = await unioContract.methods.getProductList().call();
         return result
    };
    return run();
  }
  
  


   // 查询今日累计的动态收益
   export function getMyReward(address) {    
    let run = async () => {
         ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP, {
              from:address
          });
          let result = await unioContract.methods.userInfos(address).call();
          return result
        };
    return run();
  }






 

 // 查询代币是否授权
 export function getProved(address) {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(USDT, ADDRESS_USDT, {
              from:address
          });
          let result = await unioContract.methods.allowance(address,ADDRESS_DAPP).call()/(10**18);
         return result 
    };
    return run();
  }
  export function getProveds(address) {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(USDT, ADDRESS_DRC, {
              from:address
          });
          let result = await unioContract.methods.allowance(address,ADDRESS_DAPP).call()/(10**18);
         return result 
    };
    return run();
  }
  
 // 授权USDT
 export function toApprove(address,num) {
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(USDT, ADDRESS_USDT);  
        let amount = web3s.utils.toWei(num + '','ether');
        let gas = await web3s.eth.getGasPrice();
        //console.log(gas);
        let result = await unioContract.methods.approve(ADDRESS_DAPP, amount).send({from:address, gasPrice:gas}).catch(err => {
            //console.log(err.message)
         return false
      });
      if(result){
        return result.transactionHash
    }else{
        return false
    }
    };
    return run();
  }
 // 授权ARC
 export function toApproves(address,num) {
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(USDT, ADDRESS_DRC);  
        let amount = web3s.utils.toWei(num + '','ether');
        let gas = await web3s.eth.getGasPrice();
        //console.log(gas);
        let result = await unioContract.methods.approve(ADDRESS_DAPP, amount).send({from:address, gasPrice:gas}).catch(err => {
            //console.log(err.message)
         return false
      });
      if(result){
        return result.transactionHash
        }else{
            return false
        } 
    };
    return run();
  }


//   获取用户信息
export async function getEnergy(address) {  
    let run = async () => {
        ethereum?set():""
     let web3s = new Web3(ethereum); 
     let unioContract= await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);  
 
    let result = await unioContract.methods.energys(address).call(); 
    if(result){
        // //console.log(result,'getUser');
        return  result
    }else{
        return 0
    }
  };
  return run();
}

// 绑定邀请人
export  function toBind(address,referrer){
    //console.log(address,referrer);
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
       let gas =await web3s.eth.getGasPrice()
   
        let result = await unioContract.methods.bindReferrer(referrer).send({from:address, gasPrice:gas}).catch(err => {
            //console.log(err.message)
            return false
        });
    
        if(result){
            return result.transactionHash
        }else{
            return false
        }
  };
  return run();

}
// 购买等级
export  function toBuyProduct(address,num){
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
       let gas =await web3s.eth.getGasPrice()
       //console.log(amount);
        let result = await unioContract.methods.buyProduct(num).send({from:address, gasPrice:gas}).catch(err => {
            //console.log(err.message)
            return false
        });
        if(result){
            return result.transactionHash
        }else{
            return false
        }
  };
  return run();
}
//入金
export  function toBookOrder(address,num){
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
       let gas =await web3s.eth.getGasPrice()
       let amount = web3s.utils.toWei(num + '', 'ether');
        let result = await unioContract.methods.bookOrder(amount).send({from:address, gasPrice:gas}).catch(err => {
            //console.log(err.message)
            return false
        });
        if(result){
            return result.transactionHash
        }else{
            return false
        }
  };
  return run();
}

// 获取用户订单及其索引 status 0全部 1运行中 2已出局
export async function getOrderList(address,status) {  
    let run = async () => {
        ethereum?set():""
     let web3s = new Web3(ethereum); 
     let unioContract= await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);  
    let result = await unioContract.methods.getOrders(address,status).call(); 

    if(result){
        return  result
    }else{
        return []
    }
  };
  return run();
}
    //计算用户当前收益  
    export function getAvailableOrder(address,orderId) {
   
        let run = async () => {
            ethereum?set():""
              let web3s = new Web3(ethereum);
              let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
              let result = await unioContract.methods.getAvailableOrder(address,orderId).call();
             //console.log(result);
              if(result){
                return  result
            }else{
                return 0
            }
        };
        return run();
      }
  // 用户赎回订单函数
  export async function toRedeemOrder(address,orderIndex) {  
   
    let run = async () => {
        ethereum?set():""
     let web3s = new Web3(ethereum); 
     let unioContract= await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);  
     let gas =await web3s.eth.getGasPrice()
    let result = await unioContract.methods.redeemOrder(orderIndex).send({from:address, gasPrice:gas}).catch(err => {
        //console.log(err.message)
     return false
  }); 
    if(result){
        return  result.transactionHash
    }else{
        return false
    }
  };
  return run();
}

// 查询余额
export function getMyUsdt(address) {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(USDT, ADDRESS_USDT);
          let result = await unioContract.methods.balanceOf(address).call();
          if(result){
            return  web3s.utils.fromWei(result+'', 'ether');
        }else{
            return 0
        }
    };
    return run();
  }


    //查询产品是否能购买  
    export function getUserProductCount(address,orderId) {
   
        let run = async () => {
            ethereum?set():""
              let web3s = new Web3(ethereum);
              let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
              let result = await unioContract.methods.userProductCount(address,orderId).call();
             //console.log(result);
            return  result
        };
        return run();
      }

  









// 闪兑
export  function toExChange(address,num){
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
       let gas =await web3s.eth.getGasPrice()
       let amount = web3s.utils.toWei(num + '','ether');
        let result = await unioContract.methods.exChange(amount).send({from:address, gasPrice:gas}).catch(err => {
           //console.log(err.message)
            return false
        });
        if(result){
            return result.transactionHash
        }else{
            return false
        }
  };
  return run();
}

// 提现
export  function toWithdrawal(address){
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
       let gas =await web3s.eth.getGasPrice()
        let result = await unioContract.methods.withdrawal().send({from:address, gasPrice:gas}).catch(err => {
           //console.log(err.message)
            return false
        });
        if(result){
            return result.transactionHash
        }else{
            return false
        }
  };
  return run();
}
// 排行榜
export async function getRankUsersList() {  
    let run = async () => {
        ethereum?set():""
     let web3s = new Web3(ethereum); 
     let unioContract= await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);  
    let result = await unioContract.methods.getRankUsers().call(); 
    if(result){
        return  result
    }else{
        return []
    }
  };
  return run();
}
//计算用户当前可领取的私募余额   
export function getAvailableEnergy(address) {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
          let result = await unioContract.methods.getAvailableEnergy(address).call();
          if(result){
            return  result
        }else{
            return 0
        }
    };
    return run();
  }

  //用户提现可领取的私募   
  export function getWithdrawEnergy(address) {
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);  
        // let amount = web3s.utils.toWei(num + '','ether');
        let gas = await web3s.eth.getGasPrice();
        let result = await unioContract.methods.withdrawEnergy().send({from:address, gasPrice:gas}).catch(err => {
            //console.log(err.message)
         return false
      });
      if(result){
        return result.transactionHash
    }else{
        return false
    }
    };
    return run();
  }
 //用户提现可领取的资产  
 export function getwithdrawal(address) {
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);  
        // let amount = web3s.utils.toWei(num + '','ether');
        let gas = await web3s.eth.getGasPrice();
        let result = await unioContract.methods.withdrawal().send({from:address, gasPrice:gas}).catch(err => {
            //console.log(err.message)
         return false
      });
      if(result){
        return result.transactionHash
    }else{
        return false
    }
    };
    return run();
  }
  // 私募划转
  export  function toTransfertEnergy(address,toAddress,num){
      let run = async () => {
          ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
         let gas =await web3s.eth.getGasPrice()
         let amount = web3s.utils.toWei(num + '', 'ether');
         //console.log(toAddress,amount);
          let result = await unioContract.methods.transfertEnergy(toAddress,amount).send({from:address, gasPrice:gas}).catch(err => {
             //console.log(err.message)
              return false
          });
          if(result){
              return result.transactionHash
          }else{
              return false
          }
    };
    return run();
  }
// 划转
export  function toTransferRepay(address,num){
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
       let gas =await web3s.eth.getGasPrice()
       let amount = web3s.utils.toWei(num + '', 'ether');
        let result = await unioContract.methods.transferRepay(amount).send({from:address, gasPrice:gas}).catch(err => {
           //console.log(err.message)
            return false
        });
        if(result){
            return result.transactionHash
        }else{
            return false
        }
  };
  return run();
}

  
// 用户创建余额宝订单函数
export  function toBookYuEOrder(address,num){
    let run = async () => {
        ethereum?set():""
        let web3s = new Web3(ethereum);
        let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
       let gas =await web3s.eth.getGasPrice()
       //console.log(gas);
       let amount = web3s.utils.toWei(num + '', 'ether');
        let result = await unioContract.methods.bookYuEOrder(amount).send({from:address, gasPrice:gas}).catch(err => {
            //console.log(err.message)
            return false
        });
        if(result){
            return result.transactionHash
        }else{
            return false
        }
  };
  return run();
}
// 查询价格
export function getPrice(address) {    
    let run = async () => {
         ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP, {
              from:address
          });
          let amount = web3s.utils.toWei(1 + '', 'ether');
          let result = await unioContract.methods.getDrcTokenOfUsdt(amount).call();  
          if(result){
            return  web3s.utils.fromWei(result+'', 'ether');
        }else{
            return 0
        }
    };
    return run();
  }
  
  //用户提现可领取的私募   
  export function getYuEOrders(address,status) {
    //console.log(address);
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(DAPP, ADDRESS_DAPP);
          let result = await unioContract.methods.getYuEOrders(address,status).call();
         //console.log(result);
          if(result){
            return result;
        }else{
            return []
        }
    };
    return run();
  }




//用户提现可领取的私募   
export function getBSnum(address,status) {
    let run = async () => {
        ethereum?set():""
          let web3s = new Web3(ethereum);
          let unioContract = await new web3s.eth.Contract(USDT, ADDRESS_USDT);
          let result = await unioContract.methods.balanceOf(address).call();
          if(result){
            return  web3s.utils.fromWei(result+'', 'ether');
        }else{
            return 0
        }
    };
    return run();
  }



























